
const MAP={
	// '/address':'address_controller',
	// '/order':'order_controller',
	'/session':'session_controller',
	// '/user':'user_controller',
	// '/member':'member_controller',
	'/visitor':'visitor_controller',
	'/category':'category_controller',
	'/object':'object_controller',
	'/record':'record_controller',
	// '/utils':'utils_controller',
		
	}
	
class JQBaseController {
	
    constructor(url,conn,req_obj,no_check) {
		if(url){
			let parts=url.split('?');
			if(parts)this.url=parts[0];
			if(parts.length>1)this.params=parts[1];
			
		}
        this.conn=conn;
		this.req_obj = req_obj;
		this.no_check=no_check;
    }
	
    async dispatch(){ }
	async isValid(){
		
		
		if(!this.no_check){
			console.log('is valid' + JSON.stringify(this.req_obj));
			let req_obj= this.req_obj;
			if(!req_obj.session_token)return false;
			let users_table = global.JQTableIF.instance('login_session_table',this.conn);
			let session_obj = await users_table.queryOne(null,['session_token=',this.req_obj.session_token]);
			if(!session_obj)return false;
			this.req_obj.visitor_id= session_obj.visitor_id;
		
			if(req_obj.data){
				let data_obj = global.JQRequest.parseFromData(req_obj.data);
				
				req_obj.data_obj=data_obj;
				
				
			}	
			
		
			return true;
			
		}else{
			return true;
		}
		
		
	}
	static async router(url,data, callback){

		//url= '/'+url[1];
	//	如果有回调则处理
		if(callback){
			let conn = null;
		
			try{
				let first_path= null;
				let paths=null;
				if(url)paths= url.split('/');
				if(paths && paths.length>1){
					first_path = '/'+ paths[1];
				}else{
					first_path = '/';
				}
				let func = MAP[first_path];
				
				conn = await  global.JQMysqlConnection.instance();
				
				if(!conn)console.log('de error');
				
				//callback('jqsmm handle result11:://' + first_path);
				const JQBaseController = !func?null:require(`./subs/${func}`);
					
				if(JQBaseController){
				
					let req_obj = global.JQRequest.parseFromData(data);
					
					let controller = new JQBaseController(url,conn,req_obj);
					let is_valid = await controller.isValid();
					if(!is_valid){
						callback(JQResponse.makeErrorResponse(global.consts.Error.NEED_LOGIN,
						    global.consts.Message.NEED_LOGIN));
						return;
					}
					console.log('router 1111');
					let res_data = await controller.dispatch();
					console.log('router 2222');
					let response = null;
					console.log('router 3333');
					if(!res_data)response =global.JQResponse.makeErrorResponse(global.consts.Error.ERROR_PARAM,
						global.consts.Message.ERROR_PARAM);
					else response = global.JQResponse.makeResponse(res_data);	 
					callback(response);
				}else{
					let error_response = global.JQResponse.makeErrorResponse(global.consts.Error.ERROR_OP,
					   global.consts.Message.UNKNOW_OP);
					// callback(JQResponse.makeErrorResponse(global.consts.Error.NEED_LOGIN
					//    global.consts.Message.NEED_LOGIN));
					//callback()
					callback(error_response);
				}
				
			}catch(e){
				
			    console.log('controller router error ' + e.message);
				let code = e.code;
				let message = e.message;
				if(!code){
					code = global.consts.Error.NEED_LOGIN;
					message = global.consts.Message.NEED_LOGIN;
				}
				let error_response = JQResponse.makeErrorResponse(code,
				   message);
				callback(error_response);
			}
			finally{
				if(conn){
					console.log('db realease::' +222);
					conn.releaseDB();
				}else{
					console.log('db realead::' +333);
				}
				
			}
			
			//const controller = require()
	//		通过调用request模块的pareseRequest方法将data转换为request结构的对象
			
	// 		var obj = request.parserRequest(data);
	// 		if(!obj.seq){
	// 			obj.seq = new Date().getTime();
	// 		}
	// 		var link = partner.split('/'); //代理商 级别以/分开如果只有/则为信马由缰xmyj
	// 		var single='jqepay';
	
	// 		console.log('request::'+JSON.stringify(obj));
	// 		if(link.length>0){
	// 			single = link[link.length-1];
	// 			if(single.length<=0)single='jqepay';
	// 		}
			
	// //		调用handleService处理路由功能
	// 		async function cb(res){
	// //			调用response的toResonse方法生成Response对象的json字符串
	// 			var string = response.toResponse(res);
				
	// 			if(string && string.length>2048){
	// 				console.log('response11::  '+ res.seq +'::'+  string.substring(0,2048));
	
	// 			}else{
	// 				console.log('response:: '+ res.seq +'::'+ string);
	
	// 			}
	// 			console.log('page  '+consts.Service.LOGIN);
	
	// 			//await delay(4000);
	
	// 			callback(string);
			
	// 		};
			
	// 		handleService(single,obj,cb);
		
		}
	}
}
module.exports = JQBaseController;